package main

import (
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/astaxie/beego/orm"
	_ "github.com/mattn/go-sqlite3"
	_ "beego-demo/routers" /*注册路由的*/
	"beego-demo/models"
)

func init() {
	orm.RegisterDriver("sqlite", orm.DRSqlite)
	orm.RegisterDataBase("default", "sqlite3", "datas.db")
	orm.RunSyncdb("default", false, true)
}

func main() {

	o := orm.NewOrm()
	o.Using("default")
	stu := new(models.Student)

	stu.Name = "bei"
	stu.Age = 25
	stu.Sex = "m"
	stu.Score = 88
	stu.Addr = "hunan.leiyang"

	fmt.Println(o.Insert(stu))

	perfile := new(models.Profile)
	perfile.Age = 30

	user := new(models.User)
	user.Name = "tom"
	user.Profile = perfile

	// insert
	o.Insert(perfile)
	o.Insert(user)
	o.Insert(perfile)
	o.Insert(user)
	o.Insert(perfile)
	o.Insert(user)

	// update
	user.Name = "hezhixiong"
	num, err := o.Update(user)
	fmt.Printf("NUM: %d, ERR: %v\n", num, err)

	// delete
	o.Delete(&models.User{Id: 2})

	/*设置模板路径*/
	//beego.SetViewsPath("views")
	/*
	* 设置静态文件路径
	* beego框架默认放在static目录下面，放在别的地方，项目无法访问。
	* 可以通过SetStaticPath在项目中设置静态路径
	*/
	//beego.SetStaticPath("/public", "public")
	beego.SetStaticPath("/", "static")
	beego.Run()
}
//https://www.jianshu.com/p/540be59eab74